<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="工厂:" prop="tmBasPlantId">
            <el-select v-model="formModel.tmBasPlantId" clearable filterable>
              <el-option
                v-for="item in plantOptions"
                :key="item.tmBasPlantId"
                :label="item.plantNo+'-'+item.plantNameC"
                :value="item.tmBasPlantId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="VIN年份:" prop="vinYearKey">
            <hey-select v-model="formModel.vinYearKey" :clearable="false" clcd="Key_Vin_ModeYear" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始序号:" prop="startNo">
            <el-input v-model.number="formModel.startNo" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最大序号:" prop="maxNo">
            <el-input v-model.number="formModel.maxNo" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'SupplierMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      plantOptions: [],
      formModel: {
        vinSerialNumId: '',
        tmBasPlantId: '',
        vinYearKey: '',
        startNo: undefined,
        maxNo: undefined,
        useStatus: ''
      },
      // 老数据
      oldFormModel: {
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
    this.getPlantList()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.oldFormModel = JSON.parse(JSON.stringify(this.formModel))
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        tmBasPlantId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        vinYearKey: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        startNo: this.HEY_VALIDATOR({ type: ['MAXINT[6]'], required: true }),
        maxNo: this.HEY_VALIDATOR({ type: ['MAXINT[6]'], required: true })
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    // 获取工厂信息列表
    getPlantList() {
      const param = {}
      param.isDelete = '0'
      param.status = '1'
      this.get('/tmBasPlant/queryTmBasPlantList', param).then(res => {
        this.plantOptions = res
      })
    },
    // 提交新增或修改
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.formModel.vinSerialNumId !== '') {
            console.log('新数据' + JSON.stringify(this.formModel))
            console.log('老数据' + JSON.stringify(this.oldFormModel))
            // 判断是否修改
            if (JSON.stringify(this.formModel) !== JSON.stringify(this.oldFormModel)) {
              const tip = '修改成功！'
              const url = '/vinSerialNum/updateVinSerialNum'
              this.post(url, this.formModel).then(res => {
                this.$info(tip, () => {
                  this.$emit('mntSuc', res)
                  this.closeDialog()
                })
              })
            } else {
              this.$emit('mntSuc')
              this.closeDialog()
            }
          } else {
            const url = '/vinSerialNum/saveVinSerialNum'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
